package com.lin.utils;


import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.util.DisplayMetrics;
import android.view.WindowManager;

/**
 * 手机屏幕管理
 * 
 * @author kapai8
 * 
 */
public class WindowUtil {
	private static DisplayMetrics mDisplayMetrics;

	private static WindowUtil mWindowManger;

	public static synchronized WindowUtil getWindowManger(Context mContext) {
		if(mWindowManger == null){
			mWindowManger = new WindowUtil();
			WindowManager wm = ((Activity) mContext).getWindowManager();
			mDisplayMetrics = new DisplayMetrics();
			wm.getDefaultDisplay().getMetrics(mDisplayMetrics);
		}
		return mWindowManger;
	}


	/**
	 * 获取手机屏幕宽度
	 * 
	 * @param mContext
	 * @return
	 */
	public int getWinWidth() {
		return mDisplayMetrics.widthPixels;
	}

	/**
	 * 获取手机屏幕高度
	 * 
	 * @param mContext
	 * @return
	 */
	public int getWinHeight() {
		return mDisplayMetrics.heightPixels;
	}

	/**
	 * @param dimen
	 *            组件的水平间距
	 * @return 全屏GridView中numColumns为3的组件尺寸
	 */
	public int getGridItemIconSize(float dimen) {
		return (int) ((mDisplayMetrics.widthPixels - 4 * dimen) / 3);
	}

	/**
	 * @param dimen
	 *            组件的水平间距
	 * @return 浮层GridView中numColumns为3的组件尺寸
	 */
	public int getSuperGridItemIconSize(float dimen) {
		return (int) ((getSuperWindowWidth() - 4 * dimen) / 3);
	}

	public DisplayMetrics getDisplayMetrics() {
		return mDisplayMetrics;
	}

	/**
	 * @return 浮层窗口的宽度
	 */
	public int getSuperWindowWidth() {
		return (int) (mDisplayMetrics.widthPixels * 6 / 7.0f);
	}

	/**
	 * @return 浮层窗口的高度
	 */
	public int getSuperWindowHeight() {
		// 如果是横屏
		if (mDisplayMetrics.widthPixels > mDisplayMetrics.heightPixels) {
			return (int) (mDisplayMetrics.heightPixels * 6 / 7.0f);
		} else {
			return (int) (mDisplayMetrics.heightPixels * 2 / 3f);
		}
	}

	/**
	 * @return 是否是竖屏，TRUE表示竖屏
	 */
	public boolean isPortrait(Context mContext) {

		Configuration mConfiguration = mContext.getResources().getConfiguration(); // 获取设置的配置信息
		int ori = mConfiguration.orientation; // 获取屏幕方向

		if (ori == Configuration.ORIENTATION_LANDSCAPE) {

			// 横屏
			return true;
		} else if (ori == Configuration.ORIENTATION_PORTRAIT) {

			// 竖屏
			return false;
		}
		return false;
	}
}
